<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input  v-model="query.name" placeholder="分类名称" class="filter-item" style="width: 500px;margin-right: 5px;" ></el-input>
      <el-button @click="getRecords" type="primary" class="filter-item">查询</el-button>
      <el-button @click="openAddDialog" type="primary" class="filter-item">添加</el-button>
    </div>

    <el-table
      style="width: 100%"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="selection"
        width="55" />
      <el-table-column prop="id" label="ID"  />
      <el-table-column prop="name" label="文章名称" />
      <el-table-column prop="created_at" label="创建时间"  />
      <el-table-column prop="updated_at" label="更新时间"  />
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 100, 200]"
      :page-size="pagination.pageSize"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>

    <el-dialog title="添加文章分类" :visible.sync="dialogFormVisible" :before-close="closeAddDialog">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item v-if="isAdd">
          <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
          <el-button @click="closeAddDialog" >取消</el-button>
        </el-form-item>
        <el-form-item v-else>
          <el-button type="primary" @click="onSubmit('form')">修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import { myGet, myPost } from '@/utils/myRequest'

const urlClassifyLst = '/guard/article/classify/lst'
const urlClassifyAdd = '/guard/article/classify/add'
const urlClassifyDel = '/guard/article/classify/del'
const urlClassifyDetail = '/guard/article/classify/detail'

export default {


  data() {
    return {
      list: [],
      isAdd:true,
      dialogFormVisible:false,
      listLoading: false,
      query:{
        name:''
      },
      pagination:{
        page:1,
        pageSize:10,
        total:0,
      },
      form:{
        id: 0,
        name: '',
      },
      rules:{
        name:[
          { required: true, message: '必填', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    reload(){
      Object.assign(this.$data.query,this.$options.data().query)
      Object.assign(this.$data.pagination,this.$options.data().pagination)
      this.getRecords()
    },

    getRecords(){
      this.listLoading = true
      let params = Object.assign(this.query,this.pagination)
      myGet(urlClassifyLst,params).then(res=>{
        this.listLoading = false;
        if(res.status) {
          this.list = res.data.records
          this.total = res.data.total
        }
      })
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          myPost(urlClassifyAdd,this.form).then(res => {
            this.dialogFormVisible = false
            if(res.status){
              this.$message.success(res.msg)
              this.reload()
            }
          })
        }
      })
    },
    openAddDialog(){
      this.dialogFormVisible = true
      Object.assign(this.$data.form,this.$options.data().form)
    },
    handleEdit(index,row){
      let id = row.id
      myGet(urlClassifyDetail,{id}).then(res=>{
        if(res.status){
          this.isAdd = false
          this.openAddDialog()
          Object.assign(this.form,res.data.records[0])
        }
      })
    },

    handleDelete(index,row){
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let id = row.id
        myGet(urlClassifyDel,{id}).then(res => {
          if(res.status){
            this.$message.success(res.msg)
            this.getRecords()
          }
        })
      });
    },
    closeAddDialog(){
      this.dialogFormVisible= false
      Object.assign(this.$data.form,this.$options.data().form)
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.pagination.page = 1
      this.getRecords()
    },
    handleCurrentChange(val) {
      this.pagination.page  = val
      this.getRecords();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }

  },
  mounted() {
    this.getRecords();
  },
}
</script>

<style scoped>

</style>
